//
//  gKEIjmJLCFWillPayViewControllermzyydi.m
//  Jlcf_ios
//
//  Created by zl on 2018/10/18.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "gKEIjmJLCFWillPayViewControllermzyydi.h"

#import "JLCFTableViewCell.h"
#import "NcnbyJJLCFWillPayInformationTableViewCellTRZRlN.h"
#import "WdoaAoJLCFWillPayRulerTableViewCellcaiIvs.h"
#import "WBCRhGJLCFWillPayRedPacketTableViewCellTtKZMv.h"
#import "QIxfosJLCFHBCollectionViewCellWxsVao.h"
#import "XvmSEfJLCFListRequestdkZIyb.h"
#import "XZHNOcJLCFMineBindCardViewControllerMRXeuQ.h"
#import "LdalypJLCFListPayOrderViewControllerRSNbkl.h"
#import "FozWsFJLCFMineWelfareDataModelLetMnJ.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "zCTafxJLCFListYuePayViewControllerFzNQRu.h"
//#import "QTZApplicationEntrance.h"

@interface gKEIjmJLCFWillPayViewControllermzyydi ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    NSString *_amount;
    NSInteger _pagenumber;
}



@property (nonatomic, strong) UIImageView *rRHNlxheaderViewfJQeRl;

@property (nonatomic, strong) UIButton *buyBtn;
@property (nonatomic, strong) NSMutableArray *hbArr;

//选中的优惠券
@property (nonatomic, strong) FozWsFJLCFMineWelfareDataModelLetMnJ *welfare;
@property (nonatomic, strong) NSIndexPath *HbPWpschooseindexlNtNHR;

@end

@implementation gKEIjmJLCFWillPayViewControllermzyydi

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hbArr= [[NSMutableArray alloc]init];
    self.view.frame = CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT);
    self.navigationBar.title = @"支付";
    self.tableView.frame = CGRectMake(0, NAVBAR_HEIGHT, FULL_WIDTH, FULL_HEIGHT - (IPHONEX ? 70 : 40));
    self.tableView.mj_header.hidden = YES;
    self.tableView.mj_footer.hidden = YES;
    self.tableView.backgroundColor = QTZ_Gray237;
    self.tableView.allowsSelection = NO;
    
    self.buyBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, FULL_HEIGHT - (IPHONEX ? 70 : 40), FULL_WIDTH, 40)];
    self.buyBtn.titleLabel.font = FONT(15);
    [self.buyBtn setBackgroundColor:Theme_Color];
    [self.buyBtn setTitle:@"立刻出借" forState:0];
    [self.buyBtn setTitleColor:QTZ_White forState:0];
    [self.buyBtn addTarget:self action:@selector(handleBuy) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buyBtn];
    
//    self.rRHNlxheaderViewfJQeRl = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 250 + (IPHONEX ? 24 : 0))];
//    self.rRHNlxheaderViewfJQeRl.image = [UIImage imageNamed:@"icon_listDetail_bg"];
//    self.tableView.tableHeaderView = self.rRHNlxheaderViewfJQeRl;
    
//    [self addNavigationBar];
    
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)handleBuy{
    
    //首先确定是否走余额支付
    if([[JLCFRegisterService shareService].userMoney integerValue]>=[_amount integerValue]){
        //跳转到余额支付页面
        zCTafxJLCFListYuePayViewControllerFzNQRu *vc  = [zCTafxJLCFListYuePayViewControllerFzNQRu new];
        vc.amount = _amount;
        vc.proId = self.CUWORhdetailModelIRREMd.data.id;
        if (self.welfare) {
            vc.welfare = self.welfare;
        }
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
    }else{//跳转到银行卡支付
        //银行卡支付之前首先判断是否有银行卡可以使用
        [sHkPtEJLCFMineRequestozAYZs myCardWithparams:nil loadingInView:self.view success:^(SssHPaJLCFMYCardInfoModelHkAXvP *data) {
            if ([data.isBind isEqualToString:@"0"]) {
                //前去绑定银行卡
                XZHNOcJLCFMineBindCardViewControllerMRXeuQ *vc = [XZHNOcJLCFMineBindCardViewControllerMRXeuQ new];
                vc.flFGIcbindCardmirovY = YES;
                vc.bindComplete = ^{
                    [self handleBuy];
                };
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
            }else{
                //前去支付
                LdalypJLCFListPayOrderViewControllerRSNbkl *vc = [LdalypJLCFListPayOrderViewControllerRSNbkl new];
                vc.cardinfo = data;
                vc.proid = self.CUWORhdetailModelIRREMd.data.id;
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
                vc.amount = self->_amount;
                if(self.welfare){
                    vc.welfare = self.welfare;
                }
                
            }
        } failure:^(NSString *error) {
            [self showNotice:error];
        }];
    }
    
//    [ListRequest toBorrowOrderWithParams:@{@"borrowId" : self.CUWORhdetailModelIRREMd.data.id} loadingInView:self.view success:^(RpJUgMJLCFListOrderDataModelYevCTI *order) {
//
//        if ([order.type isEqualToString:@"0"]) {//未绑卡
//            XZHNOcJLCFMineBindCardViewControllerMRXeuQ *vc = [XZHNOcJLCFMineBindCardViewControllerMRXeuQ new];
//            vc.flFGIcbindCardmirovY = YES;
//            vc.bindComplete = ^{
//                [self handleBuy];
//            };
//            [[[JLCFApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
//        } else {
//            LdalypJLCFListPayOrderViewControllerRSNbkl *vc = [LdalypJLCFListPayOrderViewControllerRSNbkl new];
//
////            vc.borrowName = self.bxiKZJproTitlehvcALF;
//            [[[JLCFApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
//            vc.order = order;
//            vc.amount = self->_amount;
//            if(self.welfare){
//                vc.welfare = self.welfare;
//            }
//        }
//
//    } failure:^(NSString *error) {
//        [self showNotice:error];
//    }];
}


-(void)loadData{
//    self.title = @"支付";
//    NSLog(  @"repost          ------------------------");
    
    [self.hbArr removeAllObjects];
    _pagenumber = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.CUWORhdetailModelIRREMd.data.id forKey:@"borrowId"];
    [params setSafeObject:@"10000000" forKey:@"buyAmt"];
    [params setSafeObject:@"hblist" forKey:@"type"];
    [params setSafeObject:@(_pagenumber) forKey:@"page"];
    
    if([JLCFRegisterService shareService].hasToken){
        [XvmSEfJLCFListRequestdkZIyb welfareWith:params loadingInView:self.view success:^(qBSjUSJLCFListWelfareDataModelmtizAG *welfare) {
            
            if (welfare.hblist.count > 0) {
                [self configRedPacketWithArr:welfare.hblist];
            } else {
                [self showNoMoreDataNotice:@"没有更多了"];
            }
            [self.tableView reloadData];
            [self endRefresh];
        } failure:^(NSString *error) {
            [self endRefresh];
            [self showNotice:error];
        }];
    }
    
}

-(void)configRedPacketWithArr:(NSArray *)arr{
    for (FozWsFJLCFMineWelfareDataModelLetMnJ *welfare in arr) {
//        if ([welfare.hbtype isEqualToString:@"1"]) {
            [self.hbArr addSafeObject:welfare];
//        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    } else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 37;
    } else if(indexPath.section == 1) {
        return 220;
    }else{
        return 190;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 10)];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 14;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 10)];
    rRHNlxheaderViewfJQeRl.backgroundColor = [UIColor clearColor];
    return rRHNlxheaderViewfJQeRl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section == 0 ? 15 : 0;
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    if (indexPath.section == 1 || indexPath.section == 2) {
    
    if ([cell respondsToSelector:@selector(tintColor)]) {
        
        if (tableView == self.tableView) {
            
            CGFloat cornerRadius = 10.f;
            
            cell.backgroundColor = UIColor.clearColor;
            
            CAShapeLayer *layer = [[CAShapeLayer alloc] init];
            
            CGMutablePathRef pathRef = CGPathCreateMutable();
            
            CGRect bounds = CGRectInset(cell.bounds, 10, 0);
            
            if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
                
            } else if (indexPath.row == 0) {
                
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
                
            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
                
            } else {
                
                CGPathAddRect(pathRef, nil, bounds);
                
            }
            
            layer.path = pathRef;
            
            CFRelease(pathRef);
            
            layer.fillColor = [UIColor colorWithWhite:1.f alpha:1.f].CGColor;
            
            
            UIView *testView = [[UIView alloc] initWithFrame:bounds];
            
            [testView.layer insertSublayer:layer atIndex:0];
            
            testView.backgroundColor = UIColor.clearColor;
            
            cell.backgroundView = testView;
            
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NcnbyJJLCFWillPayInformationTableViewCellTRZRlN *cell = [NcnbyJJLCFWillPayInformationTableViewCellTRZRlN dequeueReusableCellForTableView:self.tableView];
        if (indexPath.row == 0) {
            [cell passRowId:indexPath.row andInfo: self.CUWORhdetailModelIRREMd.data.remmoney];
        }else if(indexPath.row == 1){
            [cell passRowId:indexPath.row andInfo:[JLCFRegisterService shareService].userMoney];
        }
        [cell reloadData];
        return cell;
    }
    
    if (indexPath.section == 1) {
        WdoaAoJLCFWillPayRulerTableViewCellcaiIvs *cell = [WdoaAoJLCFWillPayRulerTableViewCellcaiIvs dequeueReusableCellForTableView:tableView];
        cell.CUWORhdetailModelIRREMd = self.CUWORhdetailModelIRREMd;
        cell.umUnmiamount = ^(NSString *umUnmiamount) {
            self->_amount = umUnmiamount;
            self.welfare = nil;
//            self.HbPWpschooseindexlNtNHR = nil;
            
//            [self reloadHB];
            NSIndexPath *indexPath= [NSIndexPath indexPathForRow:0 inSection:2];
            WBCRhGJLCFWillPayRedPacketTableViewCellTtKZMv *tempCell = [tableView cellForRowAtIndexPath:indexPath];
            
//            [tempCell.KtNdmqcollectionviewRizTgm reloadData];
            if (self.HbPWpschooseindexlNtNHR != nil) {
                QIxfosJLCFHBCollectionViewCellWxsVao *cell = (QIxfosJLCFHBCollectionViewCellWxsVao *)[tempCell.KtNdmqcollectionviewRizTgm cellForItemAtIndexPath:self.HbPWpschooseindexlNtNHR];
                cell.backgroundColor = [UIColor clearColor];
                
                self.HbPWpschooseindexlNtNHR = nil;
            }
        };
        cell.ruleEnd = ^{
            
        };
        [cell reloadData];
        return cell;
    }
    
    WBCRhGJLCFWillPayRedPacketTableViewCellTtKZMv *cell = [WBCRhGJLCFWillPayRedPacketTableViewCellTtKZMv dequeueReusableCellForTableView:tableView];
    
    
    cell.KtNdmqcollectionviewRizTgm.delegate = self;
    cell.KtNdmqcollectionviewRizTgm.dataSource = self;
    [cell.KtNdmqcollectionviewRizTgm registerClass:[QIxfosJLCFHBCollectionViewCellWxsVao class] forCellWithReuseIdentifier:@"collectCell"];
//    cell.id = self.CUWORhdetailModelIRREMd.data.id;
//    cell.amount = @"1000000";
    [cell reloadData];
    return cell;
    
}

//-(void)reloadHB{
////    NSLog(@"h  hbreload   hb   h bh bh bh bh b");
//
//    if ([[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]] isKindOfClass:[WillPayRedPacketTableViewCell class]]) {
//        [[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]] performSelector:@selector(postRedPacket) withObject:nil];
//    }
//}



//
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.hbArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    QIxfosJLCFHBCollectionViewCellWxsVao *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectCell" forIndexPath:indexPath];
    cell.layer.borderColor=Theme_Color.CGColor;
    cell.layer.borderWidth=.3f;
    cell.welfare = [self.hbArr safeObjectAtIndex:indexPath.row];
    
    [cell loadDat];
    if (self.HbPWpschooseindexlNtNHR == indexPath) {
        cell.backgroundColor = [UIColor lightGrayColor];
    } else {
        cell.backgroundColor = [UIColor clearColor];
    }
        
//    if (!cell) {
//         HBCollectionViewCell * cell = [HBCollectionViewCell alloc]initwith;
//        [cell de]
//    }
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FozWsFJLCFMineWelfareDataModelLetMnJ *temmodel =[self.hbArr safeObjectAtIndex:indexPath.row];
    if([self.CUWORhdetailModelIRREMd.data.remmoney integerValue] < [temmodel.smoney integerValue]){
        [self showNotice:@"标的剩余可买金额不足"];
        return;
    }else{
        if ([_amount integerValue] <  [temmodel.smoney integerValue]) {
            [self showNotice:@"购买金额低于红包限额"];
        }else{
            if (self.HbPWpschooseindexlNtNHR) {
                QIxfosJLCFHBCollectionViewCellWxsVao *cell = (QIxfosJLCFHBCollectionViewCellWxsVao *)[collectionView cellForItemAtIndexPath:self.HbPWpschooseindexlNtNHR];
                cell.backgroundColor = [UIColor clearColor];
                
            }
            QIxfosJLCFHBCollectionViewCellWxsVao *cell = (QIxfosJLCFHBCollectionViewCellWxsVao *)[collectionView cellForItemAtIndexPath:indexPath];
            cell.backgroundColor = [UIColor lightGrayColor];
            
            //改变购买金额
            //绑定红包id
            self.HbPWpschooseindexlNtNHR = indexPath;
            self.welfare = [self.hbArr safeObjectAtIndex:self.HbPWpschooseindexlNtNHR.row];
        }
        
    }
    
    
    
    
}

            


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view PzfRvYcontrollerLrnibE using [segue destinationViewController].
    // Pass the selected object to the new view PzfRvYcontrollerLrnibE.
}
*/

@end
